package com.sakila.action.store;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sakila.action.util.JsonUtil;
import com.sakila.pojo.Store;
import com.sakila.pojo.other.Page;
import com.sakila.service.StoreService;
import com.sakila.vo.StoreInventoryVo;

@Controller
@RequestMapping(value={"/", "/staff"})
public class StoreAction {
	
	private StoreService storeService;
	
	@Autowired
	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}

	//hyper link jumping only
	@RequestMapping(value="/viewStore")
	public String viewList(Model model){
		return "store/store";
	}
	
	//get data in json
	@RequestMapping(value="/selectStore")
	@ResponseBody
	public Object getJson(){
		List<Store> list = storeService.findAll(new Page());
		return JsonUtil.toJson(list);
	}
	
	@RequestMapping(value="/viewFilmInventory")
	@ResponseBody
	public Object viewFilmInventory(@RequestParam(defaultValue="false") Boolean detail,
									@RequestParam Integer filmId){
		if(detail){
			List<StoreInventoryVo> sivs = storeService.findInventoryByFilm(filmId);
			return JsonUtil.toJson(sivs);
		}
		List<Store> list = storeService.findByFilm(filmId);
		return JsonUtil.toJson(list);
	}
}
